package control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import model.Liuyanban;
import util.Pagination;
import db.DBConnect;

public class LiuyanbanControl {
	public static Pagination pageInstance = null;
	
	/**
	 * 增加
	 * @author 
	 * 2012-02-28
	 */
	public boolean insertLiuyanban(Liuyanban liuyanban)
	{
		DBConnect dbc = new DBConnect();
		String sql ="insert into liuyanban (liuyanName,liuyanBiaoti,liuyanNeirong) values (?,?,?)";
		
		dbc.prepareStatement(sql);
		
		try {
			dbc.setString(1, liuyanban.getLiuyanName());
			dbc.setString(2, liuyanban.getLiuyanBiaoti());
			dbc.setString(3, liuyanban.getLiuyanNeirong());
					
			dbc.executeUpdate();
			return true;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally {
			dbc.close();
		}
	}
	
	
	/**
	 * 根据id获取信息
	 * @author 
	 * 2012-02-23
	 */
	
	public Liuyanban GetLiuyanban(int id)
	{
		DBConnect dbc = new DBConnect();
		String sql = "select * from liuyanban where liuyanId = ?";

		dbc.prepareStatement(sql);
		
		try {
			dbc.setInt(1, id);
			ResultSet rs = dbc.executeQuery();
			Liuyanban liuyanban = new Liuyanban();
			
			if(rs.next())
			{
				liuyanban.setLiuyanName(rs.getString("liuyanName"));
				liuyanban.setLiuyanBiaoti(rs.getString("liuyanBiaoti"));
				liuyanban.setLiuyanNeirong(rs.getString("liuyanNeirong"));
			
				return liuyanban;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbc.close();
		}
		return null;
	}
	
	
	/**
	 * 删除
	 * @author dujuan
	 * 2012-02-23
	 */
	
	public boolean deleteLiuyanban(int id)
	{
		DBConnect dbc = new DBConnect();
		String sql = "delete from liuyanban where liuyanId =?";
		
		dbc.prepareStatement(sql);
		
		try {
			dbc.setInt(1, id);
			dbc.executeUpdate();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
		
	}
	
	/**
	 * 修改
	 * @author dujuan
	 * 2012-02-23
	 */
	
	public boolean updateLiuyanban (Liuyanban liuyanban, int id)
	{
		DBConnect dbc = new DBConnect();
		String sql = "update liuyanban set liuyanName=?,liuyanBiaoti=?,liuyanNeirong=?  where liuyanId =?";
		dbc.prepareStatement(sql);
		
		try {
			dbc.setString(1, liuyanban.getLiuyanName());
			dbc.setString(2, liuyanban.getLiuyanBiaoti());
			dbc.setString(3, liuyanban.getLiuyanNeirong());
			dbc.setInt(4, id);
			
			dbc.executeUpdate();
			return true;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
	}
	
	
	/**
	 * 列表返回显示总结
	 * 
	 */
	
	public Collection GetLiuyanban()
	{
		DBConnect dbc = new DBConnect();
		String sql = "select * from liuyanban order by liuyanId desc";
		dbc.prepareStatement(sql);
		Collection<Liuyanban> shetuanList = new ArrayList<Liuyanban>();
		try {
			ResultSet rs = dbc.executeQuery();
			while(rs.next())
			{
				
				Liuyanban liuyanban = new Liuyanban();
				liuyanban.setLiuyanId(rs.getInt("liuyanId"));
				liuyanban.setLiuyanName(rs.getString("liuyanName"));
				liuyanban.setLiuyanBiaoti(rs.getString("liuyanBiaoti"));
				liuyanban.setLiuyanNeirong(rs.getString("liuyanNeirong"));
			}
			return shetuanList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbc.close();
		}
		
		return null;
	}
	


	/*
	 * 返回总结列表，分页显示
	 */
	
	public Collection GetLiuyanban(int page, int rowsPerPage)
	{
		String sql = "select * from liuyanban order by liuyanId desc";
		pageInstance = new LiuyanbanControlPage(sql,rowsPerPage);
		
		try {
			return pageInstance.getPage(page);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;

	}
	

class LiuyanbanControlPage extends Pagination{
	public LiuyanbanControlPage(String sql, int rowsPerPage)
	{
		super(sql,rowsPerPage,1);
	}
	
	@Override
	protected Collection<Liuyanban> packResultSet(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		Collection<Liuyanban> liuyanbanList = new ArrayList<Liuyanban>();
		while(rs.next())
		{
			Liuyanban liuyanban = new Liuyanban();
			
			liuyanban.setLiuyanId(rs.getInt("liuyanId"));
			liuyanban.setLiuyanName(rs.getString("liuyanName"));
			liuyanban.setLiuyanBiaoti(rs.getString("liuyanBiaoti"));
			liuyanban.setLiuyanNeirong(rs.getString("liuyanNeirong"));
			
				
			liuyanbanList.add(liuyanban);
			liuyanban = null;
			}
			return liuyanbanList;
			
		}
	
	}
	
}
	


	
	
	

	

	
	
